var app = getApp();
Page({
  data: {
    code:'',
  },
  goBack: function () {
            wx.switchTab({
              url: '/page/LookProduct/index/index',
            })
  },
  onShow: function () {

    let self = this
    wx.login({
      success: function (res) {
        if (res.code) {
          console.log(res.code)
          self.setData({
            code: res.code
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
    // wx.getSetting({
    //   success: res => {
    //     if (res.authSetting['scope.userInfo']) {
    //       console.log("已授权")
    //       wx.showLoading({ title: "启动中..." })
    //       var that = this
    //       var cookie = wx.getStorageSync("cookie")
    //       console.log("cookie:" + cookie)
    //       if (!cookie || cookie == "") {
    //         wx.login({
    //           success: res => {
    //             this.login(res.code)
    //           }
    //         })
    //       } else {
    //         wx.switchTab({
    //           url: '/page/LookProduct/index/index',
    //         })
    //       }
    //       // wx.login({
    //       //   success: res => {
    //       //     this.loginPostPony(res.code)
    //       //   }
    //       // })
    //     } else {
    //       //显示授权按钮
    //     }
    //   }
    // })
  },

  // bindGetUserInfo: function (e) {
  //   //用户按了允许授权按钮
  //   if (e.detail.userInfo) {
  //     this.onShow()
  //   } else {
  //     wx.showToast({ title: "授权失败，请重试", icon: "none", duration: 2000 })
  //   }
  // },
  // login(code) {
  //   console.log('重新登录')
  //   var that = this
  //   wx.getUserInfo({
  //     withCredentials: true,
  //     success: res => {
  //       console.log("heheh")
  //       console.log(res)
  //       app.globalData.AlreadyJumpedLogin = true;
  //       wx.setStorageSync('nickName', JSON.parse(res.rawData).nickName)
  //       app.request({
  //         url: 'api/wechat/getWeChatMiniUserByCode',
  //         method: "POST",
  //         data: { code: code, encryptedData: res.encryptedData, iv: res.iv },
  //         success: function (data, header) {
  //           wx.hideLoading()
  //           if (data.data && data.data.result) {
  //             console.log(header["Set-Cookie"])
  //             var index1 = header["Set-Cookie"].indexOf("wechatAuthTokenV2")
  //             var index2 = header["Set-Cookie"].indexOf(";", index1)
  //             var cookie = header["Set-Cookie"].substring(index1, index2)

  //             var usernameKey = "DisplayUserName"
  //             var index3 = header["Set-Cookie"].indexOf(usernameKey) + usernameKey.length + 1
  //             var index4 = header["Set-Cookie"].indexOf(";", index3)
  //             var username = header["Set-Cookie"].substring(index3, index4)
  //             wx.setStorageSync('cookie', cookie)
  //             wx.setStorageSync('username', username)
  //             wx.switchTab({
  //               url: '/page/LookProduct/index/index',
  //             })
  //           } else {
  //           }
  //         },
  //         complete: function (res) {
  //           app.globalData.AlreadyJumpedLogin = false
  //         }
  //       })
  //     }
  //   })
  // },
  registlogin(e){
    let self = this
    app.request({
      url: 'api/wechat/getPhoneNumber',
      data: {
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv,
        code: self.data.code
      },
      method: "post",
      success: function (data, header) {
        wx.hideLoading()
        if (data.data && data.data.result) {
          app.globalData.AlreadyJumpedLogin = true;
            console.log(header["Set-Cookie"])
            // var index1 = header["Set-Cookie"].indexOf("wechatAuthTokenV2")
            // var index2 = header["Set-Cookie"].indexOf(";", index1)
            // var cookie = header["Set-Cookie"].substring(index1, index2)

            var usernameKey = "DisplayUserName"
            var index3 = header["Set-Cookie"].indexOf(usernameKey) + usernameKey.length + 1
            var index4 = header["Set-Cookie"].indexOf(";", index3)
            var username = header["Set-Cookie"].substring(index3, index4)
          wx.setStorageSync('cookie', header["Set-Cookie"])
            wx.setStorageSync('username', username)
            console.log(data.data.info)
          if (data.data.info.company != null && data.data.info.company != '') {
            wx.switchTab({
              url: '/page/LookProduct/index/index',
            })
          } else {
            wx.navigateTo({
              url: '/page/UserCenter/addCompany/index',
            })
          }
        }
      },
      fail:function(){
        wx.showToast({ title: "请再试一次", icon: "none", duration: 2000 })
      }
    })
  },
  getPhoneNumber: function (e) {
    var that = this;
        console.log(e.detail.errMsg);
        if (e.detail.errMsg == "getPhoneNumber:ok") {
              that.registlogin(e)
        }
        else if (e.detail.errMsg === 'getPhoneNumber:fail user deny') { //拒绝授权
          wx.showToast({
            title: '您拒绝了授权',
            icon: 'none',
            duration: 2000
          })

        } else if (e.detail.errMsg === 'getPhoneNumber:fail 用户未绑定手机，请先在微信客户端进行绑定后重试') {
          wx.showToast({
            title: '您的微信未绑定手机号',
            icon: 'none',
            duration: 3000
          })
        }
      
  }
})